package com.qf.kdxf.service.impl;

/**
 * @Author 明廷盛
 * @Date 2025/6/16 18:09
 * @Description
 */
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.qf.kdxf.entity.ResultData;
import com.qf.kdxf.entity.naturallanguage.JsonParse;

import com.qf.kdxf.service.NaturalLanguageService;
import com.qf.kdxf.util.NaturalLanguageUtil;
import org.springframework.stereotype.Service;
import java.util.Base64;
@Service
public class NaturalLanguageServiceImpl implements NaturalLanguageService {
    // JSON解析
    private static final Gson gson=new Gson();
    @Override
    public ResultData getTranlation(String from, String to, String text) {
        JSONObject jsonObject = null;
        try {
            String result = NaturalLanguageUtil.doRequest(from, to, text);
            JsonParse myJsonParse = gson.fromJson(result, JsonParse.class);
            String textBase64Decode=new String(Base64.getDecoder().decode(myJsonParse.getPayload().getResult().getText()), "UTF-8");
            jsonObject = JSON.parseObject(textBase64Decode);
            System.out.println(jsonObject);
            System.out.println("text字段Base64解码后=>"+jsonObject.getString("trans_result"));
        } catch (Exception e) {
            return new ResultData(501,"请输入正确的格式");
        }
        return new ResultData(200,jsonObject.getString("trans_result"));
    }
}
